package com.aiti.lulian.service.impl;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.util.StrUtil;
import com.aiti.base.core.utils.message.Message;
import com.aiti.lulian.dto.DataBoardDto;
import com.aiti.lulian.entity.CommunicateInfo;
import com.aiti.lulian.mapper.DataBoardMapper;
import com.aiti.lulian.service.DataBoardService;
import com.aiti.lulian.service.ICommunicateInfoService;
import com.aiti.lulian.utils.DateFilterUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.orm.jpa.AutoConfigureTestEntityManager;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class DataBoardServiceImpl implements DataBoardService {
    @Autowired
    private DataBoardMapper dataBoardMapper;
    @Override
    public Message top5(DataBoardDto dto) {
        String userId = dto.getUserId();
        String roleId = dto.getRoleId();
        String enterpriseId = dto.getCompanyId();
        if(!StringUtils.isEmpty(roleId)){
            String[] split = roleId.split(StrUtil.COMMA);
            boolean systemAdmin = Arrays.stream(split).filter(f->"1".equals(f)).count() > 0;
            dto.setSysAdmin(systemAdmin);
            if(StringUtils.isEmpty(enterpriseId) && !systemAdmin){
                return Message.fail("用户企业为空，请联系管理员");
            }
            if(!systemAdmin){
                boolean enterpriseAdmin = isEnterpriseAdmin(roleId);
                dto.setEnterpriseAdmin(enterpriseAdmin);
            }

        }else{
            return Message.fail("用户角色为空");
        }
        List<Map> maps = dataBoardMapper.top5(dto);
        return Message.success(maps);
    }

    @Override
    public Message businessFunnel(DataBoardDto dto) {
        String userId = dto.getUserId();
        String roleId = dto.getRoleId();
        String enterpriseId = dto.getCompanyId();
        if(!StringUtils.isEmpty(roleId)){
            String[] split = roleId.split(StrUtil.COMMA);
            boolean systemAdmin = Arrays.stream(split).filter(f->"1".equals(f)).count() > 0;
            dto.setSysAdmin(systemAdmin);
            if(StringUtils.isEmpty(enterpriseId) && !systemAdmin){
                return Message.fail("用户企业为空，请联系管理员");
            }
            if(!systemAdmin){
                boolean enterpriseAdmin = isEnterpriseAdmin(roleId);
                dto.setEnterpriseAdmin(enterpriseAdmin);
            }
        }else{
            return Message.fail("用户角色为空");
        }
        List<Map> maps = dataBoardMapper.businessFunnel(dto);
        return Message.success(maps);
    }

    @Override
    public Message customerStatus(DataBoardDto dto) {
        String userId = dto.getUserId();
        String roleId = dto.getRoleId();
        String enterpriseId = dto.getCompanyId();
        if(!StringUtils.isEmpty(roleId)){
            String[] split = roleId.split(StrUtil.COMMA);
            boolean systemAdmin = Arrays.stream(split).filter(f->"1".equals(f)).count() > 0;
            dto.setSysAdmin(systemAdmin);
            if(StringUtils.isEmpty(enterpriseId) && !systemAdmin){
                return Message.fail("用户企业为空，请联系管理员");
            }
            if(!systemAdmin){
                boolean enterpriseAdmin = isEnterpriseAdmin(roleId);
                dto.setEnterpriseAdmin(enterpriseAdmin);
            }
        }else{
            return Message.fail("用户角色为空");
        }
        List<Map> maps = dataBoardMapper.customerStatus(dto);
        return Message.success(maps);
    }

    @Override
    public Message businessSource(DataBoardDto dto) {
        String userId = dto.getUserId();
        String roleId = dto.getRoleId();
        String enterpriseId = dto.getCompanyId();
        if(!StringUtils.isEmpty(roleId)){
            String[] split = roleId.split(StrUtil.COMMA);
            boolean systemAdmin = Arrays.stream(split).filter(f->"1".equals(f)).count() > 0;
            dto.setSysAdmin(systemAdmin);
            if(StringUtils.isEmpty(enterpriseId) && !systemAdmin){
                return Message.fail("用户企业为空，请联系管理员");
            }
            if(!systemAdmin){
                boolean enterpriseAdmin = isEnterpriseAdmin(roleId);
                dto.setEnterpriseAdmin(enterpriseAdmin);
            }
            boolean enterpriseAdmin = isEnterpriseAdmin(roleId);
            dto.setEnterpriseAdmin(enterpriseAdmin);
        }else{
            return Message.fail("用户角色为空");
        }
        List<Map> maps = dataBoardMapper.businessSource(dto);
        return Message.success(maps);
    }

    @Override
    public Message contractConfirmCount(DataBoardDto dto) {
        String userId = dto.getUserId();
        String roleId = dto.getRoleId();
        String enterpriseId = dto.getCompanyId();
        if(!StringUtils.isEmpty(roleId)){
            String[] split = roleId.split(StrUtil.COMMA);
            boolean systemAdmin = Arrays.stream(split).filter(f->"1".equals(f)).count() > 0;
            dto.setSysAdmin(systemAdmin);
            if(StringUtils.isEmpty(enterpriseId) && !systemAdmin){
                return Message.fail("用户企业为空，请联系管理员");
            }
            if(!systemAdmin){
                boolean enterpriseAdmin = isEnterpriseAdmin(roleId);
                dto.setEnterpriseAdmin(enterpriseAdmin);
            }
            boolean enterpriseAdmin = isEnterpriseAdmin(roleId);
            dto.setEnterpriseAdmin(enterpriseAdmin);
        }else{
            return Message.fail("用户角色为空");
        }
        List<Map> maps = dataBoardMapper.contractConfirmCount(dto);
        return Message.success(maps);
    }

    @Override
    public Message customerDistribution(DataBoardDto dto) {
        String userId = dto.getUserId();
        String roleId = dto.getRoleId();
        String enterpriseId = dto.getCompanyId();
        if(!StringUtils.isEmpty(roleId)){
            String[] split = roleId.split(StrUtil.COMMA);
            boolean systemAdmin = Arrays.stream(split).filter(f->"1".equals(f)).count() > 0;
            dto.setSysAdmin(systemAdmin);
            if(StringUtils.isEmpty(enterpriseId) && !systemAdmin){
                return Message.fail("用户企业为空，请联系管理员");
            }
            if(!systemAdmin){
                boolean enterpriseAdmin = isEnterpriseAdmin(roleId);
                dto.setEnterpriseAdmin(enterpriseAdmin);
            }
            boolean enterpriseAdmin = isEnterpriseAdmin(roleId);
            dto.setEnterpriseAdmin(enterpriseAdmin);
        }else{
            return Message.fail("用户角色为空");
        }
        List<Map> maps = dataBoardMapper.customerDistribution(dto);
        return Message.success(maps);
    }

    @Override
    public Message visitorCount(DataBoardDto dto) {
        String userId = dto.getUserId();
        String roleId = dto.getRoleId();
        String enterpriseId = dto.getCompanyId();
        if(!StringUtils.isEmpty(roleId)){
            String[] split = roleId.split(StrUtil.COMMA);
            boolean systemAdmin = Arrays.stream(split).filter(f->"1".equals(f)).count() > 0;
            dto.setSysAdmin(systemAdmin);
            if(StringUtils.isEmpty(enterpriseId) && !systemAdmin){
                return Message.fail("用户企业为空，请联系管理员");
            }
            if(!systemAdmin){
                boolean enterpriseAdmin = isEnterpriseAdmin(roleId);
                dto.setEnterpriseAdmin(enterpriseAdmin);
            }
            boolean enterpriseAdmin = isEnterpriseAdmin(roleId);
            dto.setEnterpriseAdmin(enterpriseAdmin);
        }else{
            return Message.fail("用户角色为空");
        }
        List<Map> maps = dataBoardMapper.visitorCount(dto);
        return Message.success(maps);
    }

    @Override
    public Message customerFollowUp(DataBoardDto dto) {
        String userId = dto.getUserId();
        String roleId = dto.getRoleId();
        String enterpriseId = dto.getCompanyId();
        if(!StringUtils.isEmpty(roleId)){
            String[] split = roleId.split(StrUtil.COMMA);
            boolean systemAdmin = Arrays.stream(split).filter(f->"1".equals(f)).count() > 0;
            dto.setSysAdmin(systemAdmin);
            if(StringUtils.isEmpty(enterpriseId) && !systemAdmin){
                return Message.fail("用户企业为空，请联系管理员");
            }
            if(!systemAdmin){
                boolean enterpriseAdmin = isEnterpriseAdmin(roleId);
                dto.setEnterpriseAdmin(enterpriseAdmin);
            }
            boolean enterpriseAdmin = isEnterpriseAdmin(roleId);
            dto.setEnterpriseAdmin(enterpriseAdmin);
        }else{
            return Message.fail("用户角色为空");
        }
        Map oldMap = new HashMap();
        dto.setOldFlag(false);
        List<Map> oldList = dataBoardMapper.customerFollowUp(dto);
        oldMap.put("isOld",true);
        oldMap.put("list",oldList);
        dto.setOldFlag(true);
        List<Map> newList = dataBoardMapper.customerFollowUp(dto);
        Map newMap = new HashMap();
        newMap.put("isOld",false);
        newMap.put("list",newList);
        List resultList = new ArrayList();
        resultList.add(oldMap);
        resultList.add(newMap);
        return Message.success(resultList);
    }

    @Override
    public Message saleInfo(DataBoardDto dto) {
        String userId = dto.getUserId();
        String roleId = dto.getRoleId();
        String enterpriseId = dto.getCompanyId();
        if(!StringUtils.isEmpty(roleId)){
            String[] split = roleId.split(StrUtil.COMMA);
            boolean systemAdmin = Arrays.stream(split).filter(f->"1".equals(f)).count() > 0;
            dto.setSysAdmin(systemAdmin);
            if(StringUtils.isEmpty(enterpriseId) && !systemAdmin){
                return Message.fail("用户企业为空，请联系管理员");
            }
            if(!systemAdmin){
                boolean enterpriseAdmin = isEnterpriseAdmin(roleId);
                dto.setEnterpriseAdmin(enterpriseAdmin);
            }
            boolean enterpriseAdmin = isEnterpriseAdmin(roleId);
            dto.setEnterpriseAdmin(enterpriseAdmin);
        }else{
            return Message.fail("用户角色为空");
        }
        String dateFilterType = dto.getDateFilterType();
        if(!StringUtils.isEmpty(dateFilterType)){
            DateFilterUtil.getBeginAndEnd(dateFilterType, dto);
        }
        Map maps = dataBoardMapper.saleInfo(dto);
        return Message.success(maps);
    }

    @Override
    public Message cardPubInfo(DataBoardDto dto) {
        String userId = dto.getUserId();
        String roleId = dto.getRoleId();
        String enterpriseId = dto.getCompanyId();
        if(!StringUtils.isEmpty(roleId)){
            String[] split = roleId.split(StrUtil.COMMA);
            boolean systemAdmin = Arrays.stream(split).filter(f->"1".equals(f)).count() > 0;
            dto.setSysAdmin(systemAdmin);
            if(StringUtils.isEmpty(enterpriseId) && !systemAdmin){
                return Message.fail("用户企业为空，请联系管理员");
            }
            if(!systemAdmin){
                boolean enterpriseAdmin = isEnterpriseAdmin(roleId);
                dto.setEnterpriseAdmin(enterpriseAdmin);
            }
            boolean enterpriseAdmin = isEnterpriseAdmin(roleId);
            dto.setEnterpriseAdmin(enterpriseAdmin);
        }else{
            return Message.fail("用户角色为空");
        }
        List<Map> maps = dataBoardMapper.cardPubInfo(dto);
        return Message.success(maps);
    }

    @Override
    public Message cardPubTop5(DataBoardDto dto) {
        String userId = dto.getUserId();
        String roleId = dto.getRoleId();
        String enterpriseId = dto.getCompanyId();
        if(!StringUtils.isEmpty(roleId)){
            String[] split = roleId.split(StrUtil.COMMA);
            boolean systemAdmin = Arrays.stream(split).filter(f->"1".equals(f)).count() > 0;
            dto.setSysAdmin(systemAdmin);
            if(StringUtils.isEmpty(enterpriseId) && !systemAdmin){
                return Message.fail("用户企业为空，请联系管理员");
            }
            if(!systemAdmin){
                boolean enterpriseAdmin = isEnterpriseAdmin(roleId);
                dto.setEnterpriseAdmin(enterpriseAdmin);
            }
            boolean enterpriseAdmin = isEnterpriseAdmin(roleId);
            dto.setEnterpriseAdmin(enterpriseAdmin);
        }else{
            return Message.fail("用户角色为空");
        }
        List<Map> maps = dataBoardMapper.cardPubTop5(dto);
        return Message.success(maps);
    }

    @Override
    public Message inquiryRank(DataBoardDto dto) {
        String userId = dto.getUserId();
        String roleId = dto.getRoleId();
        String enterpriseId = dto.getCompanyId();
        if(!StringUtils.isEmpty(roleId)){
            String[] split = roleId.split(StrUtil.COMMA);
            boolean systemAdmin = Arrays.stream(split).filter(f->"1".equals(f)).count() > 0;
            dto.setSysAdmin(systemAdmin);
            if(StringUtils.isEmpty(enterpriseId) && !systemAdmin){
                return Message.fail("用户企业为空，请联系管理员");
            }
            if(!systemAdmin){
                boolean enterpriseAdmin = isEnterpriseAdmin(roleId);
                dto.setEnterpriseAdmin(enterpriseAdmin);
            }
            boolean enterpriseAdmin = isEnterpriseAdmin(roleId);
            dto.setEnterpriseAdmin(enterpriseAdmin);
        }else{
            return Message.fail("用户角色为空");
        }
        List<Map> maps = dataBoardMapper.inquiryRank(dto);
        return Message.success(maps);
    }

    @Override
    public Message inquiryCount(DataBoardDto dto) {
        String userId = dto.getUserId();
        String roleId = dto.getRoleId();
        String enterpriseId = dto.getCompanyId();
        if(!StringUtils.isEmpty(roleId)){
            String[] split = roleId.split(StrUtil.COMMA);
            boolean systemAdmin = Arrays.stream(split).filter(f->"1".equals(f)).count() > 0;
            dto.setSysAdmin(systemAdmin);
            if(StringUtils.isEmpty(enterpriseId) && !systemAdmin){
                return Message.fail("用户企业为空，请联系管理员");
            }
            if(!systemAdmin){
                boolean enterpriseAdmin = isEnterpriseAdmin(roleId);
                dto.setEnterpriseAdmin(enterpriseAdmin);
            }
            boolean enterpriseAdmin = isEnterpriseAdmin(roleId);
            dto.setEnterpriseAdmin(enterpriseAdmin);
        }else{
            return Message.fail("用户角色为空");
        }
        List<Map> maps = dataBoardMapper.inquiryCount(dto);
        return Message.success(maps);
    }

    @Override
    public Message deptUserCount(DataBoardDto dto) {
        String userId = dto.getUserId();
        String roleId = dto.getRoleId();
        String enterpriseId = dto.getCompanyId();
        if(!StringUtils.isEmpty(roleId)){
            String[] split = roleId.split(StrUtil.COMMA);
            boolean systemAdmin = Arrays.stream(split).filter(f->"1".equals(f)).count() > 0;
            dto.setSysAdmin(systemAdmin);
            if(StringUtils.isEmpty(enterpriseId) && !systemAdmin){
                return Message.fail("用户企业为空，请联系管理员");
            }
            if(!systemAdmin){
                boolean enterpriseAdmin = isEnterpriseAdmin(roleId);
                dto.setEnterpriseAdmin(enterpriseAdmin);
            }
            boolean enterpriseAdmin = isEnterpriseAdmin(roleId);
            dto.setEnterpriseAdmin(enterpriseAdmin);
        }else{
            return Message.fail("用户角色为空");
        }
        List<Map> maps = dataBoardMapper.deptUserCount(dto);
        return Message.success(maps);
    }

    @Override
    public Message cardInfo(DataBoardDto dto) {
        String userId = dto.getUserId();
        String roleId = dto.getRoleId();
        String enterpriseId = dto.getCompanyId();
        if(!StringUtils.isEmpty(roleId)){
            String[] split = roleId.split(StrUtil.COMMA);
            boolean systemAdmin = Arrays.stream(split).filter(f->"1".equals(f)).count() > 0;
            dto.setSysAdmin(systemAdmin);
            if(StringUtils.isEmpty(enterpriseId) && !systemAdmin){
                return Message.fail("用户企业为空，请联系管理员");
            }
            if(!systemAdmin){
                boolean enterpriseAdmin = isEnterpriseAdmin(roleId);
                dto.setEnterpriseAdmin(enterpriseAdmin);
            }
            boolean enterpriseAdmin = isEnterpriseAdmin(roleId);
            dto.setEnterpriseAdmin(enterpriseAdmin);
        }else{
            return Message.fail("用户角色为空");
        }
        String dateFilterType = dto.getDateFilterType();
        if(!StringUtils.isEmpty(dateFilterType)){
            DateFilterUtil.getBeginAndEnd(dateFilterType, dto);
        }
        Map maps = dataBoardMapper.cardInfo(dto);
        return Message.success(maps);
    }

    @Override
    public Message visitorTop5(DataBoardDto dto) {
        String userId = dto.getUserId();
        String roleId = dto.getRoleId();
        String enterpriseId = dto.getCompanyId();
        if(!StringUtils.isEmpty(roleId)){
            String[] split = roleId.split(StrUtil.COMMA);
            boolean systemAdmin = Arrays.stream(split).filter(f->"1".equals(f)).count() > 0;
            dto.setSysAdmin(systemAdmin);
            if(StringUtils.isEmpty(enterpriseId) && !systemAdmin){
                return Message.fail("用户企业为空，请联系管理员");
            }
            if(!systemAdmin){
                boolean enterpriseAdmin = isEnterpriseAdmin(roleId);
                dto.setEnterpriseAdmin(enterpriseAdmin);
            }
            boolean enterpriseAdmin = isEnterpriseAdmin(roleId);
            dto.setEnterpriseAdmin(enterpriseAdmin);
        }else{
            return Message.fail("用户角色为空");
        }
        List<Map> maps = dataBoardMapper.visitorTop5(dto);
        return Message.success(maps);
    }

    private boolean  isEnterpriseAdmin(String roleId){
        String[] split = roleId.split(StrUtil.COMMA);
        long count = Arrays.stream(split).filter(f -> StringUtils.equalsAny(f, "043ea2e3e02c4aa591df1f83e65069bc", "8836c75af939406a9f41128826f6d8b3")).count();
        return count > 0;
    }



}
